* This is the do file to create "Table F1. Difference in Differences"
set seed 123

use "$path_data/temp/followup_student_parents_matched", clear

gen gend = q1d - 1

local controls i.grade gend branch1 branch2 branch3 income_source1 income_source2 income_source3 income_source4 last_income_per_member hhmember hhheadage hhheadeduyear age_tchr phone_survey


// Cog
preserve
rename followup_cog_std outcome_1
rename DT_score_pre_std_missing_0 outcome_0
reshape long outcome_, i(student_no) j(time)
reg outcome_ i.treatment##i.time DT_score_pre_std_missing_dummy cpcs_pre_std_missing_0 rosen_pre_std_missing_0 `controls', vce(boot, cluster(school_no) reps(1000))
matrix did_cog = r(table)
scalar did_cog_n = e(N) / 2
restore

// CPCS
preserve
keep if CPCS_std != .
rename CPCS_std outcome_1
rename cpcs_pre_std_missing_0 outcome_0
reshape long outcome_, i(student_no) j(time)
reg outcome_ i.treatment##i.time DT_score_pre_std_missing_0 rosen_pre_std_missing_0 `controls', vce(boot, cluster(school_no) reps(1000))
matrix did_cpcs = r(table)
scalar did_cpcs_n = e(N) / 2
restore

// RSES
preserve
keep if RSES_std != .
rename RSES_std outcome_1
rename rosen_pre_std_missing_0 outcome_0
reshape long outcome_, i(student_no) j(time)
reg outcome_ i.treatment##i.time DT_score_pre_std_missing_0 cpcs_pre_std_missing_0 `controls', vce(boot, cluster(school_no) reps(1000))
matrix did_rses = r(table)
scalar did_rses_n = e(N) / 2
restore


// significant level

local outcome cog rses cpcs

foreach dep in `outcome'{
forvalues i = 1/30 {
		if did_`dep'[4,`i']<=0.01 {
			local star_`dep'_`i' %3s "***"
		}
		else if (did_`dep'[4,`i']>0.01) & (did_`dep'[4,`i']<=0.05) {
			local star_`dep'_`i' %2s "**"
		}
		else if (did_`dep'[4,`i']>0.05) & (did_`dep'[4,`i']<=0.10) {
			local star_`dep'_`i' %1s "*"
		}
		else {
			local star_`dep'_`i'  ""
		}
}
} 

/// Table
tempname hh2
file open `hh2' using "$path_output/did.tex", write replace
file write `hh2' "" _newline
file write `hh2' "% Author: Kazuma Takakura" _newline
file write `hh2' "% Date: `c(current_date)'" _newline
file write `hh2' "% Time: `c(current_time)'" _newline
file write `hh2' "" _newline


file write `hh2' "\begin{table}[h!]\footnotesize" _newline
file write `hh2' "  \centering" _newline
file write `hh2' "  \caption{Difference in Differences}" _newline
file write `hh2' "\label{tab:did}" _newline
file write `hh2' "\scalebox{1}{" _newline
file write `hh2' "\begin{threeparttable}" _newline

file write `hh2' "\begin{tabular}{lccc}\toprule" _newline

  
file write `hh2' "  & Rapid math test score &  RSES score  & CPCS score   \\\midrule\midrule" _newline

file write `hh2' "  DiD & " %04.3f (did_cog[1,8]) `star_cog_8' "  & " %04.3f (did_rses[1,8]) `star_rses_8' " & " %04.3f (did_cpcs[1,8]) `star_cpcs_8' " \\ " _newline
file write `hh2' "    & (" %04.3f (did_cog[2,8]) ") & (" %04.3f (did_rses[2,8]) ") & (" %04.3f (did_cpcs[2,8]) ") \\ " _newline

file write `hh2' "  Treatment & " %04.3f (did_cog[1,2]) `star_cog_2' "  & " %04.3f (did_rses[1,2]) `star_rses_2' " & " %04.3f (did_cpcs[1,2]) `star_cpcs_2' " \\ " _newline
file write `hh2' "    & (" %04.3f (did_cog[2,2]) ") & (" %04.3f (did_rses[2,2]) ") & (" %04.3f (did_cpcs[2,2]) ") \\ " _newline

file write `hh2' "  After & " %04.3f (did_cog[1,4]) `star_cog_4' "  & " %04.3f (did_rses[1,4]) `star_rses_4' " & " %04.3f (did_cpcs[1,4]) `star_cpcs_4' " \\ " _newline
file write `hh2' "    & (" %04.3f (did_cog[2,4]) ") & (" %04.3f (did_rses[2,4]) ") & (" %04.3f (did_cpcs[2,4]) ") \\ " _newline

file write `hh2' "  Constant & " %04.3f (did_cog[1,27]) `star_cog_27' "  & " %04.3f (did_rses[1,26]) `star_rses_26' " & " %04.3f (did_cpcs[1,26]) `star_cpcs_26' " \\ " _newline
file write `hh2' "    & (" %04.3f (did_cog[2,27]) ") & (" %04.3f (did_rses[2,26]) ") & (" %04.3f (did_cpcs[2,26]) ") \\ " _newline

file write `hh2' "  Observations & " (did_cog_n) "  & " (did_rses_n) " & " (did_cpcs_n) " \\ " _newline

file write `hh2' "\midrule" _newline
file write `hh2' "\end{tabular}" _newline
file write `hh2' "\begin{tablenotes}" _newline
file write `hh2' "\item (a) We control student's grade, sex, baseline cognitive and baseline non-cognitive score, DT baseline time, branch dummy (location), parents' income source, last income per family member, number of household members, age of household head, education level of household head, teacher's age, sex, and phone survey dummy. " _newline
file write `hh2' "\item (b) School-clustered bootstrap standard errors are reported within parentheses. " _newline
file write `hh2' "\item (c) $^*$ Significant at 10\% level; $^{**}$ significant at 5\% level; $^{***}$ significant at 1\% level. " _newline
file write `hh2' "\end{tablenotes}" _newline
file write `hh2' "\end{threeparttable}" _newline
file write `hh2' "}" _newline
file write `hh2' "\label{tab:addlabel}%" _newline
file write `hh2' "\end{table}" _newline

file write `hh2' "" _newline
file write `hh2' "" _newline
file write `hh2' "" _newline
file write `hh2' "" _newline

file close `hh2'
